<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/page.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<title>用户等级</title>
</head>
<body>
	<jsp:include page="navigation.jsp"></jsp:include>
	<div class="container" style="margin-left: 190px; margin-bottom: 25px;">
		<div class="topbar-inner">
			<div class="myhead">
				<span class="crumb-name">等级管理</span>
			</div>
		</div>
		<form id="pageForm" action="lvinfo" method="post">
			<c:if test="${ page != null}">
				<input type="hidden" value="${ page.pageCount }" id="pageCount" name="pageCount" />
				<input type="hidden" value="${ page.pageIndex }" id="pageIndex" name="pageIndex" />
			</c:if>
		</form>
		<div class="topbar-inner">
			<div class="search">
				<div class="input-group">
					<button type="button" id="add_btn" onclick="showalert_add()" class="btn btn-default" style="margin-left: 20px; margin-top: 1px;">新增</button>
					<button type="button" id="delete_btn" onclick="toDelete()" class="btn btn-default" style="margin-left: 20px; margin-top: 1px;">删除</button>
				</div>
			</div>
		</div>

		<div style="margin: 20px 20px 20px 20px; text-align: center;">
			<table class="table table-bordered table-hover table-striped">
				<thead>
					<tr>
						<th><input type="checkbox" class="main_check"></th>
						<th>等级</th>
						<th>该等级所需经验值</th>
						<th>上一等级满级经验值</th>
						<th width="13%">操作</th>
					</tr>
				</thead>
				<c:forEach items="${ list }" var="l" varStatus="st">
					<tr>
						<form id="trForm_${ l.id }" action="lvinfodit" method="post">
							<td><input type="checkbox" class="sub_check" value="${ l.lv }"></td>
							<td>${ l.lv }</td>
							<td><input type="text" class="form-control" value="${ l.exp }" id="Exp_${l.id }" name="exp" /></td>
							<td>${ l.total }</td>
							<td width="10%"><a href="javascript:" onclick="toEdit(${ l.id })">修改</a> <c:if test="${ page.count == l.lv }"> &emsp;|&emsp; <a href="javascript:" onclick="toDelete(${ l.lv })">删除</a>
								</c:if></td>
						</form>
					</tr>
				</c:forEach>
			</table>
			<c:if test="${ page == null }">
				<div style="font-size: larger;">暂无数据</div>
			</c:if>
			<c:if test="${ page != null && page.pageCount > 1 }">
				<ul id="pagination" class="pagination">

				</ul>
				<script type="text/javascript">
                        setPage(document.getElementById("pagination"),
                                parseInt($("#pageCount").val()), parseInt($(
                                        "#pageIndex").val()));
                    </script>
			</c:if>
		</div>
	</div>
	<!-- 模态框（Modal） -->
	<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">用户等级添加</h4>
				</div>
				<div class="modal-body">
					<input type="hidden" id="modal_id" class="form-control" name="id" />
					<div style="width: 100%; height: 40px;">
						该等级需要经验值：<input type="text" id="modal_type_name" class="form-control" name="info_exp" />
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" onclick="toAdd()">提交</button>
				</div>
			</div>
		</div>
	</div>
	<!-- /.modal -->
	<script type="text/javascript">
$(function() {
    $('.submenu_2_3').addClass('submenu_native');
    $('.main_check').click(function(){
        $(".sub_check").prop("checked", this.checked);
    });
});
 

function showalert_add() {
    $("#myModal").modal('show');
}

function toEdit(index) {
    var add_exp = $('#Exp_' + index).val();
	    
    $.ajax({
        type : 'post',
        url: "lvinfoedit",
        data :  {id: index, exp: add_exp},
        success : function (data) {
            console.log(data);
            if (data != null) {
                if (data.code == 0) {
                    alert('修改成功！');
                    window.location.reload();
                } else {
                    alert(data.message);
                }
                
            }
        }
    });
}

function toAdd() {
    var add_exp = $('input[name=info_exp]').val()

    if (!isInteger(add_exp)) {
        alert('请填写正确的格式！');
        return false;
    }
    $.ajax({
        type : 'post',
        url: "lvinfoedit",
        data : {exp :add_exp}, 
        success : function (data) {
            console.log(data);
            if (data != null) {
                if (data.code == 0) {
                    alert('修改成功！');
                    window.location.reload();
                }
                
            }
        }
    });
}

function toDelete(index) {
    var lvs = index;
	if (index == null || index == '') {
		// 获取全部的checkbox
        var arr = new Array;
        var i = 0;
        $('.sub_check').each(function() {
            if($(this).prop('checked')){ 
                arr[i] = $(this).val();
                i ++;
            }
        });
        lvs = arr.join(',');
	}
	
    if (confirm("确定删除？")) {
        $.ajax({
            type : 'post',
            url: "lvinfodelete",
            data : {lvs: lvs}, 
            success : function (data) {
                console.log(data);
                if (data != null) {
                    if (data.code == 0) {
                        alert('删除成功！');
                        window.location.reload();
                    } else {
                        alert(data.message);
                    }
                    
                }
            }
        });
    }
}
</script>
</body>
</html>